//
//  CZMainViewController.swift
//  CY--weiBo
//
//  Created by mac on 15/11/23.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class CZMainViewController: UITabBarController {
    
    func composeClick() {
    //打印方法名
      print(__FUNCTION__)
    
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        tabBar.tintColor=UIColor.orangeColor()
        
        //首页
        let homeVC=CZHomeViewController()
        
        addChildViewController(homeVC, title: "首页", imageName: "tabbar_home")
        
        //消息
        let messageVC=CZMessageViewController()
        
        addChildViewController(messageVC, title: "消息", imageName: "tabbar_message_center")
        //占位控制器
        addChildViewController(UIViewController())
        
        //我的
        let profileVC=CZProfileViewController()
        
        profileVC.title="我的"
        
        profileVC.tabBarItem.image=UIImage(named: "tabbar_profile")
        
        addChildViewController(profileVC, title: "我的", imageName: "tabbar_profile")
        
        //发现
        let discoverVC=CZDiscoverViewController()
        
        addChildViewController(discoverVC, title: "发现", imageName: "tabbar_discover")

        
        
    }
    //添加撰写按钮一定要再占位控制器之后,把它覆盖
    override func viewWillAppear(animated: Bool) {
        
        super.viewWillAppear(animated)
        
        let width = tabBar.bounds.size.width / 5
        
        let frame = CGRect(x: 2 * width-5, y: 0, width: width+10, height: tabBar.bounds.size.height)
        
        composeButton.frame=frame
    }
    
    
    
    
    /**
    自定义添加tabbar子控制器
    */
    private func addChildViewController(controller: UIViewController,title: String,imageName: String) {
        
        controller.title=title
        
        controller.tabBarItem.image=UIImage(named: "imageName")
        
        //拼接选中图片名
        let selectImageName = imageName + "_highlighted"
        
        controller.tabBarItem.image = UIImage(named: selectImageName)
        
        addChildViewController(UINavigationController(rootViewController: controller))

        
    }
    //懒加载
    lazy var composeButton: UIButton = {
       let button = UIButton()
       
        //设置参数
        //设置按钮背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        //设置按钮图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        //添加到tabbar里面(这里一定要用self)
        
        self.tabBar.addSubview(button)
        //添加点击事件
        button.addTarget(self, action: "composeClick", forControlEvents: UIControlEvents.TouchUpInside)
     
       return button
    }()
    
    


}
